﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SoccerSimulator5000
{
    public partial class TeamForm : Form
    {
        public TeamForm()
        {
            InitializeComponent();

            this.Invalidate();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Font theFont = new Font( FontFamily.GenericSansSerif, 12);

            string benchString = string.Format( "{0, -12} ", "Bench" );
            string attackString = string.Format("{0, -12} {1, 3:F1}", "Attack", Game.player.playerTeam.teamData.forwardScore);
            string midfieldString = string.Format("{0, -12} {1, 3:F1}", "Midfield", Game.player.playerTeam.teamData.midfieldScore);
            string defenceString = string.Format("{0, -12} {1, 3:F1}", "Defence", Game.player.playerTeam.teamData.defenderScore);
            string goalieString = string.Format("{0, -12} {1, 3:F1}", "Goalkeeping", Game.player.playerTeam.teamData.goalieScore);


            e.Graphics.DrawString(benchString, theFont, Brushes.Black, 30, 15);
            e.Graphics.DrawString(attackString, theFont, Brushes.Black, 920, 15);
            e.Graphics.DrawString(midfieldString, theFont, Brushes.Black, 920, 180);
            e.Graphics.DrawString(defenceString, theFont, Brushes.Black, 920, 345);
            e.Graphics.DrawString(goalieString, theFont, Brushes.Black, 920, 505);

            Footballer benchDisplay = (Footballer)squadListBox.SelectedItem;
            benchDisplay.Draw(e.Graphics, 360, 35);

            Footballer attackDisplay = (Footballer)attackerListBox.SelectedItem;
            attackDisplay.Draw(e.Graphics, 916, 35);

            Footballer midfieldDisplay = (Footballer)midfielderListBox.SelectedItem;
            midfieldDisplay.Draw(e.Graphics, 916, 200);

            Footballer defendDisplay = (Footballer)defenderListBox.SelectedItem;
            defendDisplay.Draw(e.Graphics, 916, 365);

            Footballer goalieDisplay = (Footballer)goalkeeperListBox.SelectedItem;
            goalieDisplay.Draw(e.Graphics, 916, 525);

        }
        public void UpdateDataSources()
        {
            squadListBox.DataSource = null;
            squadListBox.DataSource = Game.player.playerTeam.teamData.squad.bench;
            squadListBox.DisplayMember = "displayMember";

            attackerListBox.DataSource = null;
            attackerListBox.DataSource = Game.player.playerTeam.teamData.squad.attackers;
            attackerListBox.DisplayMember = "displayMember";

            midfielderListBox.DataSource = null;
            midfielderListBox.DataSource = Game.player.playerTeam.teamData.squad.midfielders;
            midfielderListBox.DisplayMember = "displayMember";

            defenderListBox.DataSource = null;
            defenderListBox.DataSource = Game.player.playerTeam.teamData.squad.defenders;
            defenderListBox.DisplayMember = "displayMember";

            goalkeeperListBox.DataSource = null;
            goalkeeperListBox.DataSource = Game.player.playerTeam.teamData.squad.goalkeeper;
            goalkeeperListBox.DisplayMember = "displayMember";
        }

        private bool ValidateMove()
        {
            int playersOnPitch = Game.player.playerTeam.teamData.squad.attackers.Count() + Game.player.playerTeam.teamData.squad.midfielders.Count() + Game.player.playerTeam.teamData.squad.defenders.Count() + Game.player.playerTeam.teamData.squad.goalkeeper.Count();

            if (playersOnPitch < 11)
            {
                return true;
            }

            return false;
        }

        private void addAttackerButton_Click(object sender, EventArgs e)
        {
            if (ValidateMove())
            {
                try
                {
                    Footballer temp = (Footballer)squadListBox.SelectedItem;
                    Game.player.playerTeam.teamData.squad.attackers.Add(temp);
                    Game.player.playerTeam.teamData.squad.bench.Remove(temp);
                    squadListBox.SelectedItem = squadListBox.Items[0];
                    Game.player.playerTeam.teamData.UpdateData();
                    panel1.Invalidate();
                    UpdateDataSources();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception");
                    throw;
                }
                
            }
            else
                MessageBox.Show("The pitch is already full, remove a player and try again", "Full pitch");

            
        }

        private void removeAttackerButton_Click(object sender, EventArgs e)
        {
            if (Game.player.playerTeam.teamData.squad.attackers.Count() > 1)
            {
                try
                {
                    Footballer temp = (Footballer)attackerListBox.SelectedItem;
                    Game.player.playerTeam.teamData.squad.bench.Add(temp);
                    Game.player.playerTeam.teamData.squad.attackers.Remove(temp);
                    attackerListBox.SelectedItem = attackerListBox.Items[0];
                    Game.player.playerTeam.teamData.UpdateData();
                    panel1.Invalidate();
                    UpdateDataSources();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception");
                    throw;
                }
                
            }
            else
                MessageBox.Show("Must have atleast one player on attack, add another player and try again", "Can't empty position");
        }

        private void addMidfielderButton_Click(object sender, EventArgs e)
        {
            if (ValidateMove())
            {
                try
                {
                    Footballer temp = (Footballer)squadListBox.SelectedItem;
                    Game.player.playerTeam.teamData.squad.midfielders.Add(temp);
                    Game.player.playerTeam.teamData.squad.bench.Remove(temp);
                    squadListBox.SelectedItem = squadListBox.Items[0];
                    Game.player.playerTeam.teamData.UpdateData();
                    panel1.Invalidate();
                    UpdateDataSources();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception");
                    throw;
                }
                
            }
            else
                MessageBox.Show("The pitch is already full, remove a player and try again", "Full pitch");
        }

        private void removeMidfielderButton_Click(object sender, EventArgs e)
        {
            if (Game.player.playerTeam.teamData.squad.midfielders.Count() > 1)
            {
                try
                {
                    Footballer temp = (Footballer)midfielderListBox.SelectedItem;
                    Game.player.playerTeam.teamData.squad.bench.Add(temp);
                    Game.player.playerTeam.teamData.squad.midfielders.Remove(temp);
                    midfielderListBox.SelectedItem = midfielderListBox.Items[0];
                    Game.player.playerTeam.teamData.UpdateData();
                    panel1.Invalidate();
                    UpdateDataSources();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception");
                    throw;
                }
                
            }
            else
                MessageBox.Show("Must have atleast one player on the midfield, add another player and try again", "Can't empty position");
        }

        private void addDefenderButton_Click(object sender, EventArgs e)
        {
            if (ValidateMove())
            {
                try
                {
                    Footballer temp = (Footballer)squadListBox.SelectedItem;
                    Game.player.playerTeam.teamData.squad.defenders.Add(temp);
                    Game.player.playerTeam.teamData.squad.bench.Remove(temp);
                    squadListBox.SelectedItem = squadListBox.Items[0];
                    Game.player.playerTeam.teamData.UpdateData();
                    panel1.Invalidate();
                    UpdateDataSources();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception");
                    throw;
                }
                
            }
            else
                MessageBox.Show("The pitch is already full, remove a player and try again", "Full pitch");
        }

        private void removeDefenderbutton_Click(object sender, EventArgs e)
        {
            if (Game.player.playerTeam.teamData.squad.defenders.Count() > 1)
            {
                try
                {
                    Footballer temp = (Footballer)defenderListBox.SelectedItem;
                    Game.player.playerTeam.teamData.squad.bench.Add(temp);
                    Game.player.playerTeam.teamData.squad.defenders.Remove(temp);
                    defenderListBox.SelectedItem = defenderListBox.Items[0];
                    Game.player.playerTeam.teamData.UpdateData();
                    panel1.Invalidate();
                    UpdateDataSources();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception");
                    throw;
                }
                
            }
            else
                MessageBox.Show("Must have atleast one player on defence, add another player and try again", "Can't empty position");
        }

        private void changeGoalkeeperButton_Click(object sender, EventArgs e)
        {
            try
            {
                Footballer temp = (Footballer)goalkeeperListBox.SelectedItem;
                Game.player.playerTeam.teamData.squad.bench.Add(temp);
                Game.player.playerTeam.teamData.squad.goalkeeper.Remove(temp);
                
                

                temp = (Footballer)squadListBox.SelectedItem;
                Game.player.playerTeam.teamData.squad.goalkeeper.Add(temp);
                Game.player.playerTeam.teamData.squad.bench.Remove(temp);

                Game.player.playerTeam.teamData.UpdateData();
                panel1.Invalidate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception");
                throw;
            }

            panel1.Invalidate(new Rectangle(916, 525, 280, 100));
            UpdateDataSources();
        }

        private void squadListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            panel1.Invalidate(new Rectangle(360, 35, 280, 100));
        }

        private void attackerListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            panel1.Invalidate(new Rectangle(916, 35, 280, 100));
        }

        private void midfielderListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            panel1.Invalidate(new Rectangle(916, 200, 280, 100));
        }

        private void defenderListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            panel1.Invalidate(new Rectangle(916, 365, 280, 100));
        }

    }
}
